package com.baoxin.chenxixc4coach;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.java.SimpleResultHelper;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class PosterMouldActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mImg1;
    private ImageView mImg2;
    private ImageView mImg3;
    private ImageView mImg4;
    private TextView mTx1;
    private TextView mTx2;
    private TextView mTx3;
    private TextView mTx4;
    private Handler handler;

    private ArrayList<String> urls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poster_mould);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//去掉默认title
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mImg1 = findViewById(R.id.p1);
        mImg2 = findViewById(R.id.p2);
        mImg3 = findViewById(R.id.p3);
        mImg4 = findViewById(R.id.p4);
        mTx1 = findViewById(R.id.t1);
        mTx2 = findViewById(R.id.t2);
        mTx3 = findViewById(R.id.t3);
        mTx4 = findViewById(R.id.t4);
        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        urls = (ArrayList<String>) msg.obj;
                        init();
                        break;
                }
            }
        };
        try {
            NetUtil.get(this, Interfaces.URL_POSTER_LIST, new MyCallback(Interfaces.URL_POSTER_LIST, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void init() {
        GlideApp.with(this)
                .load(urls.get(0))
                .into(mImg1);
        GlideApp.with(this)
                .load(urls.get(1))
                .into(mImg2);
        GlideApp.with(this)
                .load(urls.get(2))
                .into(mImg3);
        GlideApp.with(this)
                .load(urls.get(3))
                .into(mImg4);
        mImg1.setOnClickListener(this);
        mImg2.setOnClickListener(this);
        mImg3.setOnClickListener(this);
        mImg4.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        int index = 0;
        switch (view.getId()) {
            case R.id.img1:
                index = 0;
                break;
            case R.id.img2:
                index = 1;
                break;
            case R.id.img3:
                index = 2;
                break;
            case R.id.img4:
                index = 3;
                break;
        }
        clickPoster(urls.get(index));
    }

    private void clickPoster(String posterUrl) {
        JSONObject body = new JSONObject();
        try {
            body.put("imgName", posterUrl);
            NetUtil.post(activity, Interfaces.URL_MY_POSTER, body.toString(), new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (!activityAlive()) return;
                    String result = response.body().string();
                    if (SimpleResultHelper.isSucceed(result)) {
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            final String url = jsonObject.getString("imgUrl");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showMyPoster(url);
                                }
                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(activity, "生成海报失败！", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showMyPoster(String url) {
        MyPosterActivity.startAciton(this, url);
    }

}
